/**
 * 违章审核记录 编辑页
 *
 * 作者 Wanghw
 * 日期 2024-08-14 10:38:38
 */
layui.use(['jquery','febs', 'form', 'validate','commonJS','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        fileUpload = layui.fileUpload,
        $view = $('#peccancyAuditRecord-edit'),
        commonJS = layui.commonJS,
        validate = layui.validate,
        auditTableIns,
        issuanceTableIns;

    form.verify(validate);
    form.render();
    initFormValue();
    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: '',
        type: 0,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        isDetail: true,
        done: function (res) {
        },
    });



    function initFormValue() {
        if (peccancyInformation) {
            form.val("peccancyAuditRecord-edit-form", {
                "informationId": peccancyInformation.informationId,
                "peccancyNature": peccancyInformation.peccancyNature,
                "tenantId": peccancyInformation.tenantId
            });
        }
        if(peccancyInformation.peccancyType){
            febs.initDictItemLabel($view.find('#peccancyType'),'SAFETY_PECCANCY_TYPE',peccancyInformation.peccancyType)
        }
        if(peccancyInformation.peccancyRootCause){
            febs.initDictItemLabel($view.find('#peccancyRootCause'),'SAFETY_PECCANCY_ROOT_REASON',peccancyInformation.peccancyRootCause)
        }
        if(peccancyInformation.jobOperation){
            febs.initDictItemLabel($view.find('#jobOperation'),'SAFETY_JOB_OPERATION',peccancyInformation.jobOperation)
        }
        if(peccancyInformation.disposeForm){
            febs.initDictItemLabel($view.find('#disposeForm'),'SAFETY_PECCANCY_DISPOSE_FORM',peccancyInformation.disposeForm)
        }

        if(peccancyInformation.penaltyScoreFlg && peccancyInformation.penaltyScoreFlg == '1'){
            $view.find('input[name="penaltyScore"]').removeAttr('disabled');
            $view.find('input[name="penaltyScore"]').val(peccancyInformation.penaltyScore);
            $view.find('input[name="penaltyScoreFlg"]').attr('checked','true');
        }
        if(peccancyInformation.learnVideoFlg && peccancyInformation.learnVideoFlg =='1'){
            $view.find('input[name="learnVideoFlg"]').attr('checked','true');
        }

        if(peccancyInformation.penaltyAmountFlg && peccancyInformation.penaltyAmountFlg =='1'){
            $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
            $view.find('input[name="penaltyAmount"]').val(peccancyInformation.penaltyAmount);
            $view.find('input[name="penaltyAmountFlg"]').attr('checked','true');
        }

        if(peccancyInformation.auditList){
            auditTableIns = febs.table.init({
                elem: $view.find('table[id="peccancyAuditRecordTable"]'),
                id: 'peccancyAuditRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '审核人姓名', minWidth: 80},
                        {
                            field: 'auditResult', title: '审核结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">审核通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">审核退回</span>';
                                }
                            }
                        },
                        {field: 'auditTime', title: '审核时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '审核理由', minWidth: 120 }
                    ]
                ],
                data:peccancyInformation.auditList
            });
        }

        if(peccancyInformation.issuanceList){
            issuanceTableIns = febs.table.init({
                elem: $view.find('table[id="peccancyIssuanceRecordTable"]'),
                id: 'peccancyIssuanceRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '签发人姓名', minWidth: 80},
                        {
                            field: 'auditResult', title: '签发结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">签发通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">签发退回</span>';
                                }
                            }
                        },
                        {field: 'auditTime', title: '签发时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '签发理由', minWidth: 120 },
                    ]
                ],
                data:peccancyInformation.issuanceList
            });
        }

        form.render('checkbox');
    }

    function getFileList() {
        if (peccancyInformation && peccancyInformation.fileList.length>0) {
            return  peccancyInformation.fileList;
        }else {
            return  [];
        }
    }

    // form.on('submit(peccancyAuditRecord-edit-form-submit)', function (data) {
    //     data.field.auditBusiness = '1';
    //     if(data.field.learnVideoFlg == "on"){
    //         data.field.learnVideoFlg = '1';
    //     }else{
    //         data.field.learnVideoFlg = '0';
    //     }
    //     if(data.field.penaltyAmountFlg == "on"){
    //         data.field.penaltyAmountFlg = '1';
    //         if(commonJS.isEmpty(data.field.penaltyAmount)){
    //             layer.msg('请选输入扣款金额！', {
    //                 icon: 5,
    //                 zIndex: 1989202290,
    //                 time: 1000
    //             })
    //             return false
    //         }
    //     }else{
    //         data.field.penaltyAmountFlg = '0';
    //
    //     }
    //     if(data.field.penaltyScoreFlg == "on"){
    //         data.field.penaltyScoreFlg = '1';
    //         if(commonJS.isEmpty(data.field.penaltyScore)){
    //             layer.msg('请选输入扣除分数！', {
    //                 icon: 5,
    //                 zIndex: 1989202290,
    //                 time: 1000
    //             })
    //             return false
    //         }
    //     }else{
    //         data.field.penaltyScoreFlg = '0';
    //     }
    //
    //     addOrUpdate(data.field);
    //     return false;
    // });

    form.on('checkbox(penaltyAmountFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyAmount"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyAmount"]').val('');
            $view.find('input[name="penaltyAmount"]').attr('disabled',true);
        }
        form.render()
    });

    form.on('checkbox(penaltyScoreFlg)',function (data) {
        if(this.checked){
            $view.find('input[name="penaltyScore"]').removeAttr('disabled');
        }else {
            $view.find('input[name="penaltyScore"]').val('');
            $view.find('input[name="penaltyScore"]').attr('disabled',true);
        }
    });

    form.on('submit(local-submit)', function (data) {
        data.field.auditBusiness = '1';
        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    form.on('submit(onlinePass)', function (data) {
        data.field.auditBusiness = '1';
        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }
        if(workflowApply){
            data.field.workflowApplyId=workflowApply.workflowApplyId
        }
        if(workflowNode){
            data.field.workflowNodeId=workflowApply.workflowNodeId
        }
        // data.field.status="4"
        data.field.defaultReviewResult="1"
        febs.modal.confirm('审核提示', '是否提交审核信息？', function () {
            febs.postArray(ctx + 'saveOnlineProcess', data.field, function (data) {
                if(data.code="200"){
                    layer.closeAll()
                    febs.alert.success('审核成功');
                    $('#currency-table-form').find('#query').trigger('click');
                }
            });
        });
        return false;
    });

    form.on('submit(onlineBack)', function (data) {
        if(null==data.field.auditRemark || data.field.auditRemark==""){
            layer.msg('请输入审核理由！', {
                icon: 5,
                zIndex: 1989202290,
                time: 1000
            })
            return false
        }
        data.field.auditBusiness = '1';
        if(data.field.learnVideoFlg == "on"){
            data.field.learnVideoFlg = '1';
        }else{
            data.field.learnVideoFlg = '0';
        }
        if(data.field.penaltyAmountFlg == "on"){
            data.field.penaltyAmountFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyAmount)){
                layer.msg('请选输入扣款金额！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyAmountFlg = '0';

        }
        if(data.field.penaltyScoreFlg == "on"){
            data.field.penaltyScoreFlg = '1';
            if(commonJS.isEmpty(data.field.penaltyScore)){
                layer.msg('请选输入扣除分数！', {
                    icon: 5,
                    zIndex: 1989202290,
                    time: 1000
                })
                return false
            }
        }else{
            data.field.penaltyScoreFlg = '0';
        }
        if(workflowApply){
            data.field.workflowApplyId=workflowApply.workflowApplyId
        }
        if(workflowNode){
            data.field.workflowNodeId=workflowApply.workflowNodeId
        }
        data.field.status="3"
        data.field.defaultReviewResult="2"
        febs.modal.confirm('审核提示', '是否提交审核信息？', function () {
            febs.postArray(ctx + 'saveOnlineProcess', data.field, function (data) {
                if(data.code="200"){
                    layer.closeAll()
                    febs.alert.success('审核成功');
                    $('#currency-table-form').find('#query').trigger('click');
                }
            });
        });
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('审核提示', '是否提交审核信息？', function () {
            febs.postArray(ctx + 'peccancyAuditRecord', data, function () {
                layer.closeAll();
                febs.alert.success('审核成功');
                $('#febs-peccancyAuditRecord').find('#query').trigger('click');
                $('#currency-table-form').find('#query').trigger('click');
            });
        });
    }

    form.on('radio(audit-result)',function (data) {
        var value = data.value;
        // console.log('radio'+value);
        if(value == '3'){
            $view.find('#learnFlg').addClass("febs-hide");
            $view.find('#amountFlg').addClass("febs-hide");
            $view.find('#scoreFlg').addClass("febs-hide");

            $view.find('#learnVideoFlg').prop('checked',false);
            $view.find('#penaltyAmountFlg').prop('checked',false);
            $view.find('#penaltyScoreFlg').prop('checked',false);

            $view.find('input[name="finallyPenaltyAmount"]').val('');
            $view.find('input[name="finallyPenaltyAmount"]').attr('disabled',true);
            $view.find('input[name="penaltyScore"]').val('');
            $view.find('input[name="penaltyScore"]').attr('disabled',true);
            $view.find('input[name="auditRemark"]').attr('lay-verify','');
            $view.find('input[name="auditRemark"]').attr('lay-verify','required');

            $view.find('#auditRemark').addClass("febs-form-item-require");
        }else {
            $view.find('#learnFlg').removeClass("febs-hide");
            $view.find('#amountFlg').removeClass("febs-hide");
            $view.find('#scoreFlg').removeClass("febs-hide");


            $view.find('#auditRemark').removeClass("febs-form-item-require");

        }
        form.render();
    })
    window.closePage = function () {
        layer.closeAll();
    }
});